/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.parser;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rdfparser.parser.ParserHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import rdfparser.bean.FoafBean;

/**
 *
 * @author Amendra Shrestha
 */
public class FoafParser extends ParserHandler {

    private String parsedValue;
    private FoafBean member;
    private FoafBean.MemberFriend friend;
    private boolean isPerson = false;
    private boolean isFriend = false;
    private boolean isAccount = false;

    /**
     * parses the XML document file prints the parse data of XML document file
     * adds parse data of XML document file to database
     *
     * @param baseDir
     * @param fileDir
     * @param fileName
     */
    public void parseFoaf(String fileName) throws SQLException {

        if (parseDocument(fileName)) {
            System.out.println("\tParsed document successfully.");
            printDocument();
            addData();
        } else {
            System.out.println("\tException occured while parsing document.");
        }
    }

    /**
     * overriding the default SAX start element method parses each starting XML
     * element tag and sets the required extracted data
     *
     * @param uri
     * @param localName
     * @param elementName
     * @param attributes
     */
    @Override
    public void startElement(String uri,
            String localName,
            String elementName,
            Attributes attributes) {
        if (elementName.equalsIgnoreCase("foaf:Person") && !isFriend) {
            member = new FoafBean();
            member.setPerson(attributes.getValue("rdf:about"));
            isPerson = true;
        } else if (elementName.equalsIgnoreCase("foaf:depiction")) {
            member.setDepiction(attributes.getValue("rdf:resource"));
        } else if (elementName.equalsIgnoreCase("foaf:holdsAccount")) {
            isAccount = true;
        } else if (elementName.equalsIgnoreCase("foaf:OnlineAccount")) {
            member.setOnlineAccount(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("foaf:knows")) {
            friend = member.new MemberFriend();
            isFriend = true;
        } else if (elementName.equalsIgnoreCase("foaf:Person") && isFriend) {
            friend.setFriend(attributes.getValue("rdf:about"));
        }
    }

    /**
     * overriding the default SAX end element method parses each ending XML
     * element tag and sets the required extracted data
     *
     * @param namespaceURI
     * @param localName
     * @param elementName
     */
    @Override
    public void endElement(String namespaceURI,
            String localName,
            String elementName) {
        if (elementName.equalsIgnoreCase("foaf:Person")) {
            isPerson = true;
        } else if (elementName.equalsIgnoreCase("foaf:name")) {
            member.setName(parsedValue);
        } else if (elementName.equalsIgnoreCase("foaf:nick") && isPerson && !isFriend) {
            member.setNickname(parsedValue);
            isPerson = false;
        } else if (elementName.equalsIgnoreCase("foaf:knows")) {
            member.getFriendList().add(friend);
            isFriend = true;
        } else if (elementName.equalsIgnoreCase("foaf:nick") && isFriend) {
            friend.setfriendNickname(parsedValue);
            isFriend = false;
        } else if (elementName.equalsIgnoreCase("foaf:accountName")) {
            member.setAccountName(parsedValue);
        }
    }

    /**
     * holds extracted text data in string object
     *
     * @param ch
     * @param start
     * @param length
     * @throws SAXException
     */
    @Override
    public void characters(char[] ch,
            int start,
            int length) throws SAXException {
        parsedValue = new String(ch, start, length);
    }

    /**
     * prints parsed data of XML document
     */
    private void printDocument() {
        System.out.println("Person: " + member.getPerson());
        System.out.println("Name: " + member.getName());
        System.out.println("Nickname: " + member.getNickname());
        System.out.println("Depiction: " + member.getDepiction());
        System.out.println("Online Account: " + member.getOnlineAccount());
        System.out.println("Acount Name: " + member.getAccountName());
        System.out.println("\nFrineds");
        System.out.println("--------------");
        for (FoafBean.MemberFriend f : member.getFriendList()) {
            System.out.println("Name:" + f.getFriend());
            System.out.println("Nickname:" + f.getfriendNickname() + '\n');
        }
    }

    /**
     * adds parsed data of XML document in db
     */
    private void addData() throws SQLException {
        System.out.println("Adding data to database...");
        member.addMember();
        member.addMemberFriend();
        member.addMemberAccount();
        System.out.println("Successfully completed...");
    }
}
